package ai.people.platform.archives.controller;

import ai.people.netmon.framework.domain.archives.request.WorkOrderListRequest;
import ai.people.netmon.framework.domain.archives.request.WorkOrderReplayRequest;
import ai.people.netmon.framework.domain.archives.request.WorkOrderRequest;
import ai.people.netmon.framework.domain.archives.vo.WorkOrderDetailVO;
import ai.people.netmon.framework.domain.archives.vo.WorkOrderListVO;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.archives.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工单接口
 *
 * @author zhenglin
 * @date 2022/5/31
 */
@RestController
@RequestMapping("/workOrder")
public class WorkOrderController {

    @Autowired
    WorkOrderService workOrderService;

    /**
     * 工单列表(用户)
     *
     * @param request 请求
     * @return {@link Result}<{@link PageResponse}<{@link WorkOrderListVO}>>
     */
    @PostMapping("/my/list")
    public Result<PageResponse<WorkOrderListVO>> myWorkOrderList(@Validated @RequestBody WorkOrderListRequest request) {
        PageResponse<WorkOrderListVO> list = workOrderService.myWorkOrderList(request);
        return ResultUtil.success(list);
    }

    /**
     * 工单列表(管理员)
     *
     * @param request 请求
     * @return {@link Result}<{@link List}<{@link WorkOrderListVO}>>
     */
    @PreAuthorize("hasAuthority('work:order:manage')")
    @PostMapping("/admin/list")
    public Result<PageResponse<WorkOrderListVO>> workOrderList(@Validated @RequestBody WorkOrderListRequest request) {
        PageResponse<WorkOrderListVO> list = workOrderService.adminWorkOrderList(request);
        return ResultUtil.success(list);
    }

    /**
     * 新增工单
     *
     * @param addVO 添加
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping
    public Result<Boolean> addWorkOrder(@Validated @RequestBody WorkOrderRequest addVO) {
        workOrderService.addWorkOrder(addVO);
        return ResultUtil.success();
    }

    /**
     * 工单详情
     *
     * @param id id
     * @return {@link Result}<{@link WorkOrderDetailVO}>
     */
    @GetMapping("/{id}")
    public Result<WorkOrderDetailVO> getWorkOrderDetail(@PathVariable("id") Long id) {
        WorkOrderDetailVO detailVO = workOrderService.getWorkOrderDetail(id);
        return ResultUtil.success(detailVO);
    }

    /**
     * 回复工单
     *
     * @param replayRequest 请求
     * @return {@link Result}<{@link Boolean}>
     */
    @PostMapping("/replay")
    public Result<Boolean> replay(@Validated @RequestBody WorkOrderReplayRequest replayRequest) {
        workOrderService.replay(replayRequest);
        return ResultUtil.success();
    }

    /**
     * 终止工单
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @GetMapping("/stop/{id}")
    public Result<Boolean> stopWorkOrder(@PathVariable("id") Long id) {
        workOrderService.stopWorkOrder(id);
        return ResultUtil.success();
    }

    /**
     * 受理工单
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @GetMapping("/accept/{id}")
    @PreAuthorize("hasAuthority('work:order:manage')")
    public Result<Boolean> acceptWorkOrder(@PathVariable("id") Long id) {
        workOrderService.acceptWorkOrder(id);
        return ResultUtil.success();
    }

    /**
     * 完成工单
     *
     * @param id id
     * @return {@link Result}<{@link Boolean}>
     */
    @GetMapping("/complete/{id}")
    public Result<Boolean> completeWorkOrder(@PathVariable("id") Long id) {
        workOrderService.completeWorkOrder(id);
        return ResultUtil.success();
    }

}
